//
// @Auther: by wujehy 
// @Email:wujehy@qq.com
// @Data:20-5-16
// @Time:上午9:55
//

#ifndef ASIOTEST_TCPCLIENT_H
#define ASIOTEST_TCPCLIENT_H

#include <iostream>
#include <vector>
#include <string>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/asio/ip/tcp.hpp>

#define BUFF_LEN 1024 * 64

const std::string suffix("");

class TcpClient;

typedef void (*TcpCallbackFunction)(const TcpClient *client, std::string &message);

class TcpClient {
    typedef boost::asio::ip::tcp::endpoint endpoint_type;
    typedef boost::asio::ip::address address_type;
    typedef boost::asio::ip::tcp::socket socket_type;
    typedef std::shared_ptr<socket_type> socket_ptr;
    typedef std::vector<char> buffer_type;

private:
    TcpCallbackFunction reacCallback = nullptr;
    boost::asio::io_service m_io;
    buffer_type m_buf;
    endpoint_type m_endpoint;
    boost::asio::io_service::work *work;
    socket_ptr this_socket;

public:
    void setReacCallback(TcpCallbackFunction reacCallback);

    void setWriteCallback(TcpCallbackFunction writeCallback);

    TcpClient(std::string serverip, int port);

    void start();

    void conn_handler(const boost::system::error_code &ec, socket_ptr socket);

    void read_handler(socket_ptr socket, const boost::system::error_code &ec, std::size_t bytes_transferred);

    void writemsg(std::string &msg);

    void write_handler(socket_ptr socket, const boost::system::error_code &ec);

    void run();

    void stop();
};


#endif //ASIOTEST_TCPCLIENT_H
